package com.jenvin.btrace;

import static com.sun.btrace.BTraceUtils.field;
import static com.sun.btrace.BTraceUtils.get;
import static com.sun.btrace.BTraceUtils.jstack;
import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.str;

import java.lang.reflect.Field;

import com.sun.btrace.BTraceUtils;
import com.sun.btrace.BTraceUtils.Strings;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.ProbeClassName;
import com.sun.btrace.annotations.ProbeMethodName;
import com.sun.btrace.annotations.Self;

@BTrace
public class ContextTracker {
    // /com\\.alibaba\\.service\\.template\\..*TemplateContext/

    @OnMethod(clazz = "+com.tmall.mip.ladder.LadderContext", method = "setResult", location = @Location(value = Kind.RETURN))
    public static void m(@Self Object self, @ProbeClassName String pcm, @ProbeMethodName String pmn, Object result) { // all
        // BTraceUtils.println(Strings.strcat("Context: ", Strings.strcat(pcm,
        // Strings.strcat("#", pmn))));
        // println(key);
        if (result != null) {
            println(result);
            Field idField = field("com.tmall.mip.product.dto.ProductDTO", "productId");
            Field stringField = field("com.tmall.mip.product.dto.ProductDTO", "toString");
            BTraceUtils.print(Strings.strcat("id:", str(get(idField, result))));
            BTraceUtils.print(Strings.strcat("String:", str(get(stringField, result))));
        } else {
            println("result is null");
        }

        jstack();

        // println("call CaseObject.execute");
    }
}
